﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Server
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private Socket socketSend;
        private void btnStart_Click(object sender, EventArgs e)
        {
            try
            {
                socketSend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPAddress ip = IPAddress.Parse(txtServer.Text);
                IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32(textBox2.Text));
                socketSend.Connect(point);
                ShowMessage("连接" + ip + ":" + textBox2.Text.Trim() + "成功！");

                Thread th = new Thread(Receive);
                th.IsBackground = true;
                th.Start();
            }
            catch
            {
                MessageBox.Show("网络连接错误，请重新连入！");
            }
        }
        private void ShowMessage(string msgStr)
        {
            txtLog.AppendText(msgStr + "r\n");
        }
        private void Receive(object o)
        {
           
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            if(socketSend==null||socketSend.Connected)
            {
                MessageBox.Show("网络连接错误，请重新连接！");
                return;
            }
            var str = textBox2.Text;
            byte[] buffer = Encoding.UTF8.GetBytes(str);
            socketSend.Send(buffer);
        }
    }
    
}
